package com.fc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fc.anno.Log;
import com.fc.dto.RouteDTO;
import com.fc.dto.pageQuery.RoutePageQueryDTO;
import com.fc.entity.Route;
import com.fc.result.PageResult;
import com.fc.result.Result;
import com.fc.service.IRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "线路")
@RestController
@RequestMapping("/route")
public class RouteController {

    @Autowired
    private IRouteService routeService;

    /**
     * 新增线路
     *
     * @param routeDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("新增线路")
    @PostMapping
    public Result save(@RequestBody RouteDTO routeDTO) {
        routeService.savaRoute(routeDTO);
        return Result.success();
    }

    /**
     * 修改线路
     *
     * @param routeDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("修改线路")
    @PutMapping
    public Result update(@RequestBody RouteDTO routeDTO) {
        routeService.updateRoute(routeDTO);
        return Result.success();
    }

    /**
     * 删除线路
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除线路")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        routeService.removeById(id);
        return Result.success();
    }

    /**
     * 查看全部线路列表
     *
     * @param routePageQueryDTO
     * @return
     */
    @ApiOperation("查看线路列表")
    @PostMapping("/list")
    public Result<PageResult> list(@RequestBody RoutePageQueryDTO routePageQueryDTO) {
        PageResult pageResult = routeService.listRoute(routePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查看某区域下的线路列表
     *
     * @param regionId
     * @return
     */
    @ApiOperation("查看某区域下的线路列表")
    @GetMapping("/list/{regionId}")
    public Result<List<Route>> listByRegionId(@PathVariable Long regionId) {
        QueryWrapper<Route> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("region_id", regionId);
        List<Route> routeList = routeService.list(queryWrapper);
        return Result.success(routeList);
    }


    /**
     * 获取某一条线路的详细信息
     *
     * @param id
     * @return
     */
    @ApiOperation("查看线路详细信息")
    @GetMapping("/{id}")
    public Result<Route> details(@PathVariable Integer id) {
        Route route = routeService.getById(id);
        return Result.success(route);
    }


}
